package com.qire.manhua.activity;

import android.os.Bundle;
import android.view.View;

import com.qire.antsbinder.dal.exception.DataAccessException;
import com.qire.antscore.annotation.DalExceptionObserve;
import com.qire.antscore.annotation.RouteMapping;
import com.qire.antsrouter.inject.Autowired;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.constant.Navigation;
import com.qire.common.constant.Url;
import com.qire.common.inject.EventControllerInjector;
import com.qire.common.inject.ViewModelInjector;
import com.qire.common.support.base.BaseActivity;
import com.qire.common.utils.ThemeUtil;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.snackbar.Prompt;
import com.qire.manhua.R;
import com.qire.manhua.controller.ComicQuickReadController;
import com.qire.manhua.databinding.ActivityComicQuickReadBinding;
import com.qire.manhua.model.entity.ComicBookEntity;
import com.qire.manhua.model.viewModel.impl.ComicQuickReadViewModel;
import com.qire.common.widget.animation.SimpleAnimationUtil;

@RouteMapping(path = Navigation.Page.ComicQuickReadPage)
public class ComicQuickReadActivity extends BaseActivity<ActivityComicQuickReadBinding> {

    @Autowired
    ComicBookEntity comicBookEntity;

    @Autowired(injector = EventControllerInjector.class)
    ComicQuickReadController controller;

    @Autowired(injector = ViewModelInjector.class)
    ComicQuickReadViewModel viewModel;

    @Override
    public int layoutRid() {
        return R.layout.activity_comic_quick_read;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if(ObjectUtil.isNull(comicBookEntity)) {
            ToastHelper.showToast("漫画数据错误");
            super.finish();
            return;
        }

        ThemeUtil.setWindowGravityBottom(this, 0f);
        SimpleAnimationUtil.playAnimationOnView(this, R.anim.app_dialog_bottom_in, viewDataBinder().scrollView, null);

        controller.onAddition(viewModel);
        viewModel.loadComicBookEntity(comicBookEntity);

    }

    @Override
    public void finish() {
        final ActivityComicQuickReadBinding viewDataBinder = viewDataBinder();

        SimpleAnimationUtil.playAnimationOnView(this, R.anim.app_quick_read_topbar_out, viewDataBinder.comicTitlePanel, animation -> {
            viewDataBinder.comicTitlePanel.setVisibility(View.GONE);
        });

        SimpleAnimationUtil.playAnimationOnView(this, R.anim.app_dialog_bottom_exit, viewDataBinder.scrollView, animation -> {
            super.finish();
            overridePendingTransition(0, 0);//结束的动画
        });
    }

    @DalExceptionObserve(tags = {Url.ComicQuickReadData})
    public void quickReadExceptionHandle(DataAccessException e) {
        viewModel.dismissLoadDialog();
        ToastHelper.showSnackBar(e.extraMsg, Prompt.ERROR);
    }

}
